package com.ddone.rest.controller;

import com.ddone.movie.bean.Movie;
import com.ddone.movie.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ddone
 * @date 2024/3/22-22:15
 */

@Controller
@Slf4j
@RequestMapping("/rest/movie")
public class RestMovieController {
    @Autowired
    private MovieService movieService;


    @GetMapping
    public String all(Model model){
        List<Movie> movieList = movieService.getAll();
        model.addAttribute("movieList", movieList);
        return "rest/movie/movie-list";
    }

    @DeleteMapping("/{movieId}")
    public String delete(@PathVariable String movieId){
        movieService.removeMovieById(movieId);
        return "redirect:/rest/movie";
    }

    @GetMapping("/{movieId}")
    public String get(@PathVariable String movieId,Model model){
        Movie movie = movieService.getMovieById(movieId);
        model.addAttribute("movie", movie);
        return "rest/movie/update";
    }

    @PutMapping
    public String update(Movie movie){
        movieService.updateMovie(movie);
        return "redirect:/rest/movie";
    }

    @PostMapping
    public String save(Movie movie){
        movieService.saveMovie(movie);
        return "redirect:/rest/movie";
    }

}
